package com.lwz.laboratory.controller;

import com.lwz.laboratory.entity.Department;
import com.lwz.laboratory.entity.Post;
import com.lwz.laboratory.entity.Recruitplan;
import com.lwz.laboratory.service.DepartmentService;
import com.lwz.laboratory.service.PostService;
import com.lwz.laboratory.service.RecruitplanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;

import java.util.Collection;
import java.util.List;

@Controller
public class RecruitplanController {
    @Autowired
    private RecruitplanService recruitplanService;

    @Autowired
    private PostService postService;

    @Autowired
    private DepartmentService departmentService;

//显示全部计划
    @GetMapping("/planlist")
    public String planlist(Model model, HttpServletRequest request){
        String planid = request.getParameter("context");
        List<Recruitplan> list= recruitplanService.findAllRecruitplan(planid);
        model.addAttribute("planlist",list);
        return "/plan/plan_list";
    }

    @GetMapping("/plancheck/{id}")
    public String toCheckPlan(@PathVariable("id") Integer id,Model model){
        Recruitplan recruitplan = recruitplanService.findOneRecruitplan(id);
        model.addAttribute("recruitplanlist",recruitplan);
        return "plan/plan_check";

    }

    @RequestMapping("/plancheck")
    public String CheckPlan(Recruitplan recruitplan,Model model){
        System.out.println(recruitplan.getState());
        System.out.println(recruitplan.getPersonnum());
        recruitplanService.CheckRecruitplan(recruitplan);

        return "redirect:/planlist";

    }

    @RequestMapping("/planadd")
    public String AddPlan(Recruitplan recruitplan,Model model,HttpServletRequest request){
        recruitplan.setState("0");
        recruitplanService.AddRecruitplan(recruitplan);
        return thplanlist(recruitplan.getApplicant(),model,request);

    }

    @RequestMapping("/delplan")
    public String DelPlan(Integer id,Model model,HttpServletRequest request,String person){
        recruitplanService.Delplan(id);
        return thplanlist(person,model,request);

    }

    @RequestMapping("/planpass")
    public String PlanPass(Recruitplan recruitplan,Model model){
        List<Recruitplan> list= recruitplanService.findPassRecruitplan();
        model.addAttribute("planlist",list);


        return "plan/plan_passlist";

    }


    @GetMapping("/planaddpost/{planid}")
    public String toAddPlanPost(@PathVariable("planid") String planid,Model model){
        List<Post> posts =postService.findbyplanid(planid);
        model.addAttribute("posts",posts);
        List<Recruitplan> list= recruitplanService.findAllRecruitplan(planid);
        model.addAttribute("planlist",list);

        return "plan/plan_addpost";
    }


    @RequestMapping("/postadd")
    public String AddPost(Post post,Model model){
        postService.AddPost(post);
        return toAddPlanPost(post.getPlanid(),model);

    }

    @GetMapping("/thplanlist/")
    public String thplanlist(String applicant ,Model model, HttpServletRequest request){
        String planid = request.getParameter("context");
//        List<Recruitplan> list= recruitplanService.findAllRecruitplan(planid);
        List<Recruitplan> list= recruitplanService.findbyapplicant(applicant);
        model.addAttribute("planlist",list);
        return "/plan/plan_thlist";
    }

    @GetMapping("/planadd")
    public String planadd(String departmentname,Model model, HttpServletRequest request){
        Collection<Department> department=departmentService.getAlldep(departmentname);
        model.addAttribute("departments",department);

        return "/plan/plan_add";
    }



















//    @GetMapping("/delpost/{id}")
//    public String DelPost(@PathVariable("id") Integer id,Model model){
//        postService.DelPost(id);
//
//        return "plan/plan_addpost";
//
//    }







}
